<template>
    <el-form :model="form" :inline="true">
        <el-form-item label="生产计划状态">
            <el-select v-model="form.PPState" placeholder="全部" style="width: 300px" clearable>
                <el-option label="未生成" :value="1" />
                <el-option label="部分生成" :value="2" />
                <el-option label="生成完毕" :value="3" />
            </el-select>
        </el-form-item>
        <el-form-item label="单据编号">
            <el-input v-model="form.PPDocuNumber" style="width: 300px;" clearable />
        </el-form-item>
        <el-form-item label="预生产计划名称">
            <el-input v-model="form.PPName" style="width: 300px;" clearable />
        </el-form-item>
        <el-form-item label="来源单据类型">
            <el-select v-model="form.PPType" placeholder="请选择" style="width: 300px" clearable>
                <el-option label="无来源" :value="0" />
                <el-option label="新增销售合同" :value="1" />
                <el-option label="新增维修处理" :value="2" />
                <el-option label="新增项目" :value="3" />
            </el-select>
        </el-form-item>
        <el-button type="primary" @click="GetShowTime()">查询</el-button>
        <el-button type="danger" @click="DelAll">删除</el-button>
        <el-button @click="Export()">导出</el-button>
    </el-form>
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="ppDocuNumber" label="预生产计划编号" />
        <el-table-column prop="ppName" label="预生产计划主题" />
        <el-table-column prop="ppType" label="来源单据类型">
            <template #default="scope">
                <span v-if="scope.row.ppType == 0">无来源</span>
                <span v-if="scope.row.ppType == 1">新增销售合同</span>
                <span v-if="scope.row.ppType == 2">新增维修处理</span>
                <span v-if="scope.row.ppType == 3">新增项目</span>
            </template>
        </el-table-column>
        <el-table-column prop="ppState" label="预生产计划状态">
            <template #default="scope">
                <span style="color: red;" v-if="scope.row.ppState == 1">未生成</span>
                <span style="color:darkgoldenrod" v-if="scope.row.ppState == 2">部分生成</span>
                <span style="color: green;" v-if="scope.row.ppState == 3">生成完毕</span>
            </template>
        </el-table-column>
        <el-table-column prop="pPsNum" label="本次预生产计划数量" />
        <el-table-column prop="ppGeneratedNum" label="已生产生产计划数量"></el-table-column>
        <el-table-column prop="addTime" label="制单日期">
            <template #default="scope">
                <span>{{ scope.row.addTime.substring(0, 10) }} {{ scope.row.addTime.substring(11, 19) }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                    修改
                </el-button>
                <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                    删除
                </el-button>
                <el-button size="small" type="success" v-if="scope.row.ppState!=3"
                    @click="handleshengchan(scope.$index, scope.row)">
                    生产计划
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <table>
        <tr>
            <td>
                <el-pagination v-model:current-page="Page.PageIndex" v-model:page-size="Page.PageSize"
                    :page-sizes="[5, 10, 15, 20]" :background="true" layout="slot, sizes" prev-text="上一页"
                    next-text="下一页" :total="Page.listcount" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange">
                    共{{ Page.listcount }}条数据，每页{{ Page.PageSize }}条，共{{ Page.PageIndex }}/{{ Page.pagecount }}页
                </el-pagination>
            </td>
            <td>
                <el-pagination v-model:current-page="Page.PageIndex" v-model:page-size="Page.PageSize"
                    :page-sizes="[5, 10, 15, 20]" :background="true" layout="slot, prev, pager, next" prev-text="上一页"
                    next-text="下一页" :total="Page.listcount" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange">
                    <el-button :disabled="Page.PageIndex == 1" @click="handleCurrentChange(1)">首页</el-button>
                </el-pagination>
            </td>
            <td>
                <el-pagination v-model:current-page="Page.PageIndex" v-model:page-size="Page.PageSize"
                    :page-sizes="[5, 10, 15, 20]" :background="true" layout="slot, jumper" prev-text="上一页"
                    next-text="下一页" :total="Page.listcount" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange">
                    <el-button :disabled="Page.PageIndex == Page.pagecount"
                        @click="handleCurrentChange(Page.pagecount)">尾页</el-button>
                </el-pagination>
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
const dialogVisible = ref(false)    //供应商名称弹框
import { onMounted, ref, reactive } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import axios from "axios";
import http from '../../../http.js'
const route = useRoute();
const router = useRouter();
const form = reactive({
    PPState: '',
    PPDocuNumber: '',
    PPName: '',
    PPType: '',
})
const tableData = ref([])
onMounted(() => {
    GetShowTime();
})
//分页
const Page = ref({
    PageIndex: 1,
    PageSize: 10,
    listcount: 0,
    pagecount: 0
})
const handleSizeChange = (val: number) => {
    Page.value.PageSize = val;
    GetShowTime()
}
const handleCurrentChange = (val: number) => {
    Page.value.PageIndex = val;
    GetShowTime()
}
const GetShowTime = () => {
    debugger
    axios.get(http.zblnb + "/sc/api/PreProduction/PreProductionSelectShow", {
        params: {
            PPState: form.PPState,
            PPDocuNumber: form.PPDocuNumber,
            PPName: form.PPName,
            PPType: form.PPType,
            PageIndex: Page.value.PageIndex,
            PageSize: Page.value.PageSize
        }
    }).then((res) => {
        tableData.value = res.data.data.list;
        Page.value.listcount = res.data.data.totalcount;
        Page.value.pagecount = res.data.data.pagecount;
        console.log(tableData.value)
    })
}
const handleEdit = (index: number, row: any) => {
    console.log(index, row)
    router.push({
        path: '/UpdPreProduction',
        query: {
            ids: row.preProductionId,
            preProducsId: row.preProducsId
        }
    })
}
const handleshengchan = (index: number, row: any) => {
    console.log(index, row)
    router.push({
        path: '/AddProductionPlan',
        query: {
            ids: row.preProductionId
        }
    })
}
const handleDelete = (index: number, row: any) => {
    console.log(row)
    ElMessageBox.confirm(
        '确定删除吗?',
        'Warning',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            axios.post(http.zblnb + '/sc/api/PreProduction/PreProductionDel', {
                "preProductionId": [
                    row.preProductionId
                ],
                "delName": "王思雨"
            }).then((res) => {
                if (res.data.data > 0) {
                    ElMessage.success('删除成功')
                    GetShowTime()
                }
                else {
                    ElMessage.error('删除失败')
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

const Del = reactive({
    allocateId: [],
    delName: ''
})
interface PurchaseContracts {
    purchaseContractsId: number; // 或者 string  
}
const check=ref([])
const handleSelectionChange = (val: PurchaseContracts[]) => {
    Del.allocateId = []; // 如果希望每次重置数组，请取消注释  
    check.value = val;
    val.forEach((item) => {
        if (!Del.allocateId.includes(item.preProductionId)) {
            Del.allocateId.push(item.preProductionId);
        }
    });
    console.log(Del.allocateId);
};
//批量逻辑删除
const DelAll = () => {
    ElMessageBox.confirm(
        '确定删除吗?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true,
        }
    )
        .then(() => {
            axios.post(http.zblnb + '/sc/api/PreProduction/PreProductionDel', {
                preProductionId: Del.allocateId,
                delName: "王思雨"
            }).then((res) => {
                console.log(res.data.data)
                if (res.data.data > 0) {
                    ElMessage.success('删除成功')
                    GetShowTime()
                }
                else {
                    ElMessage.error('删除失败')
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}


//#region 
//跳转供应商名称
const vender = (vendorId: Number) => {
    router.push('/ListReturnsVendor?vendorId=' + vendorId);
}
//#endregion
//导出
import * as XLSX from 'xlsx';
const Export = () => {
    ElMessageBox.confirm('是否确认导出?', '警告', {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: 'warning',
    })
        .then((res) => {
            if (check.value.length <= 0) {
                ElMessage.warning('请选择数据!');
                return;
            }
            //表头
            let tableData = [
                ['序号','预生产计划编号', '预生产计划主题', '来源单据类型', '预生产计划状态', '本次预生产计划数量','已生产生产计划数量','制单日期']
            ]
            check.value.forEach((item, index) => {
                let rowData = []
                //导出内容的字段
                rowData = [
                    index + 1,
                    item.ppDocuNumber,
                    item.ppName,
                    item.ppType,
                    item.ppState,
                    item.pPsNum,
                    item.ppGeneratedNum,
                    item.addTime,
                ]
                tableData.push(rowData)
            })
            let workSheet = XLSX.utils.aoa_to_sheet(tableData);
            let bookNew = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(bookNew, workSheet, '预生产计划') // 工作簿名称
            let name = '预生产计划' + '.xlsx'
            XLSX.writeFile(bookNew, name) // 保存的文件名
        })
}
</script>